﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Common;

namespace Business
{
    public partial class ProjectMechanicism
    {
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(
            ref string messageError,
            ref List<ProjectMechanicism> listOldPM,
            ref Common.UserInfo UserInfo
            )
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                List<ProjectMechanicism> listNewPM = new List<ProjectMechanicism>();

                foreach (var item in listOldPM)
                {
                    ProjectMechanicism pm = new ProjectMechanicism();
                    pm.ProjectID = item.ProjectID;
                    pm.ProjectRegion = item.ProjectRegion;
                    pm.Mechanicism = item.Mechanicism;
                    pm.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    pm.CreatedBy = UserInfo.UserID;
                    pm.CreatedDateTime = DateTime.Now;
                    pm.RowVersion = 1;

                    listNewPM.Add(pm);

                    context.ProjectMechanicisms.InsertOnSubmit(pm);
                }

                context.SubmitChanges();

                listOldPM = listNewPM;

                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE Update(
            ref string messageError,
            ref List<ProjectMechanicism> listOldPM,
            ref Common.UserInfo UserInfo
            )
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                List<ProjectMechanicism> listNewPM = new List<ProjectMechanicism>();

                foreach (ProjectMechanicism item in listOldPM)
                {
                    var query = context.ProjectMechanicisms.Where(a => a.ProjectID == item.ProjectID && a.ProjectRegion == item.ProjectRegion);
                    context.ProjectMechanicisms.DeleteAllOnSubmit(query);

                    ProjectMechanicism pm = new ProjectMechanicism();
                    pm.ProjectID = item.ProjectID;
                    pm.ProjectRegion = item.ProjectRegion;
                    pm.Mechanicism = item.Mechanicism;
                    pm.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    pm.CreatedBy = UserInfo.UserID;
                    pm.CreatedDateTime = DateTime.Now;
                    pm.RowVersion = 1;

                    listNewPM.Add(pm);

                    context.ProjectMechanicisms.InsertOnSubmit(pm);
                }

                context.SubmitChanges();

                listOldPM = listNewPM;

                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }
    }
}
